﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Frame.Lib.Enums;
using Frame.Lib.Common;
using Frame.Lib.Utilities;

namespace Frame.Lib.Core.Attach
{
    public class UserImage
    {
        private const int BUFFER_SIZE = 10240;
        private string _tableName;
        System.Web.HttpContext _context;

        private int _index;//同一批上传的可能时间是一样的，此变量用于区别这样的文件名

        public UserImage(string tableName)
        {
            _tableName = tableName;
            _context = System.Web.HttpContext.Current;
            _index = 0;
        }

        public UserImage(STableName tableName):this(tableName.ToString())
        {
           
        }

        public string Save(System.Web.HttpPostedFile img)
        {
            string path = GetPath(img.FileName);        
          
            Save(img, path);
            return path;
        }

        /// <summary>
        /// 保存压缩图片，长宽最大为size
        /// </summary>
        /// <param name="img"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        public string Save(System.Web.HttpPostedFile img, int size)
        {
            return this.Save(img, size, size);         
        }

        public void Save(System.Web.HttpPostedFile img, string path)
        {
            string physiPath = _context.Server.MapPath(GlobalVar.WebAppPath + path); 

            if (!Directory.Exists(System.IO.Path.GetDirectoryName(physiPath)))
            {
                Directory.CreateDirectory(System.IO.Path.GetDirectoryName(physiPath));
            }
     
            img.SaveAs(physiPath);
           
        }

        public string Save(System.Web.HttpPostedFile img, int maxWidth, int maxHeight)
        {
            string path = GetPath(img.FileName);
            System.Drawing.Bitmap imgOld = new System.Drawing.Bitmap(img.InputStream);
            int width = imgOld.Width;
            int height = imgOld.Height;
            float ratio = (float)width / (float)height;
            float maxRatio = (float)maxWidth / (float)maxHeight;
            if (ratio > maxRatio)
            {
                if (width > maxWidth)
                {
                    width = maxWidth;
                    height = (int)((float)maxWidth / ratio);
                }

            }
            else
            {
                if (height > maxHeight)
                {
                    height = maxHeight;
                    width = (int)((float)maxHeight * ratio);
                }
            }

            Save(img, width, height, path);
            return path;
        }

        public void Save(System.Web.HttpPostedFile img, int width, int height, string path)
        {
            System.Drawing.Bitmap imgOld = new System.Drawing.Bitmap(img.InputStream);
            System.Drawing.Image imgNew = CommUtil.ThumbImage(imgOld, width, height);

            string physiPath = _context.Server.MapPath(GlobalVar.WebAppPath + path);

            if (!Directory.Exists(System.IO.Path.GetDirectoryName(physiPath)))
            {
                Directory.CreateDirectory(System.IO.Path.GetDirectoryName(physiPath));
            }

            string extention = System.IO.Path.GetExtension(physiPath).Trim('.').ToLower();

            switch (extention)
            {
                case "jpg":
                case "bmp":
                    imgNew.Save(physiPath, System.Drawing.Imaging.ImageFormat.Jpeg);
                    break;
                case "png":
                    imgNew.Save(physiPath, System.Drawing.Imaging.ImageFormat.Png);
                    break;              
                default:
                    imgNew.Save(physiPath);
                    break;
            }
           
            imgNew.Dispose();

            //MemoryStream MemStream = new MemoryStream();
            //imgNew.Save(MemStream, System.Drawing.Imaging.ImageFormat.Jpeg);

            ////MemStream.WriteTo(
            //FileStream fs = new FileStream(_context.Server.MapPath(GlobalVar.WebAppPath + path), FileMode.CreateNew);
            //MemStream.WriteTo(fs);
            //MemStream.Flush();
            //fs.Flush();
        }
       
        private string GetPath(string fileName)
        {
            _index++;
            string path = AppSetting.UserImagePath + _tableName + "/" + AppSession.UserID + "_" + DateTime.Now.Ticks + "_" + _index + System.IO.Path.GetExtension(fileName);
            return path;           
        }
    }
}
